package com.ruoyi.electricity.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.electricity.domain.DTO.*;
import com.ruoyi.electricity.domain.TDataGatewaySystem;
import com.ruoyi.electricity.service.ITDataGatewaySystemService;
import com.ruoyi.electricity.service.ITElectricityDataService;
import com.ruoyi.electricity.service.ITRiskWarningsService;
import com.ruoyi.rsikManage.service.AlarmEventService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 网关数据系统Controller
 *
 * @author wu
 * @date 2023-10-28
 */
@RestController
@RequestMapping("/zdzh/tDataGatewayaSystem")
public class TDataGatewaySystemController extends BaseController {
    @Autowired
    private ITDataGatewaySystemService tDataGatewaySystemService;
    @Autowired
    private ITRiskWarningsService tRiskWarningsService;
    @Autowired
    private ITElectricityDataService itElectricityDataService;
    @Autowired
    private AlarmEventService alarmEventService;
    @Value("${myapp.start-time}")
    private String startTimeStr;

    @Value("${myapp.end-time}")
    private String endTimeStr;

    /**
     * 查询网关数据系统列表
     */
    //@PreAuthorize("@ss.hasPermi('zdzh:tDataGatewayaSystem:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDataGatewaySystem tDataGatewaySystem) {
        startPage();
        List<TDataGatewaySystem> list = tDataGatewaySystemService.selectTDataGatewaySystemList(tDataGatewaySystem);
        return getDataTable(list);
    }

    /**
     * 导出网关数据系统列表
     */
    //@PreAuthorize("@ss.hasPermi('zdzh:tDataGatewayaSystem:export')")
    @Log(title = "网关数据系统", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(TDataGatewaySystem tDataGatewaySystem, HttpServletResponse response) {
        List<TDataGatewaySystem> list = tDataGatewaySystemService.selectTDataGatewaySystemList(tDataGatewaySystem);
        ExcelUtil<TDataGatewaySystem> util = new ExcelUtil<TDataGatewaySystem>(TDataGatewaySystem.class);
        util.exportExcel(response, list, "tDataGatewayaSystem");
    }

    /**
     * 获取网关数据系统详细信息
     */
    //@PreAuthorize("@ss.hasPermi('zdzh:tDataGatewayaSystem:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDataGatewaySystemService.selectTDataGatewaySystemById(id));
    }

    /**
     * 新增网关数据系统
     */
    //@PreAuthorize("@ss.hasPermi('zdzh:tDataGatewayaSystem:add')")
    @Log(title = "网关数据系统", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDataGatewaySystem tDataGatewaySystem) {
        return toAjax(tDataGatewaySystemService.insertTDataGatewaySystem(tDataGatewaySystem));
    }

    /**
     * 修改网关数据系统
     */
    //@PreAuthorize("@ss.hasPermi('zdzh:tDataGatewayaSystem:edit')")
    @Log(title = "网关数据系统", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDataGatewaySystem tDataGatewaySystem) {
        return toAjax(tDataGatewaySystemService.updateTDataGatewaySystem(tDataGatewaySystem));
    }

    /**
     * 删除网关数据系统
     */
    //@PreAuthorize("@ss.hasPermi('zdzh:tDataGatewayaSystem:remove')")
    @Log(title = "网关数据系统", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDataGatewaySystemService.deleteTDataGatewaySystemByIds(ids));
    }


    @GetMapping("/all")
    public AjaxResult getAllData() {
        AllDataDto allData = null;
        try {
            allData = new AllDataDto();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date startTime = df.parse(startTimeStr);
            Date endTime = df.parse(endTimeStr);
            allData.setDataStatusCountList(tDataGatewaySystemService.getDataStatusCount(startTime, endTime));
            allData.setStatusCountList(tDataGatewaySystemService.getStatusCount(startTime, endTime));
            allData.setAlarmLevelCountList(tRiskWarningsService.countAlarmLevelByTime(startTime, endTime));
            allData.setTRiskWarningsList(tRiskWarningsService.getAllRiskWarnings());
            allData.setElectricityByAreaList(itElectricityDataService.getElectricityByAreaWithinTime(startTime, endTime));
            allData.setHourlyDataList(alarmEventService.getHourlyDataQuery(startTime, endTime));
            allData.setHourlyDataList1(alarmEventService.getHourlyDataQuery1(startTime, endTime));
            allData.setElectricityDataList(itElectricityDataService.getElectricityData());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return AjaxResult.success(allData);
//        //统计表中的数据状态
//        List<DataStatusCount> dataStatusCountList = tDataGatewaySystemService.getDataStatusCount(startTime, endTime);
//        List<StatusCount> statusCountList = tDataGatewaySystemService.getStatusCount(startTime, endTime);
//        // 点击详情 跳转到后台列表 展示状态开始前的字段 (仅有查询操作) 使用若依的list查询
//        //时间倒序列表的报警记录
//        // 报警表 做图表统计
//        List<AlarmLevelCount> alarmLevelCountList = tRiskWarningsService.countAlarmLevelByTime(startTime, endTime);
//        //报警记录的时间倒序列表
//        List<TRiskWarnings> tRiskWarningsList = tRiskWarningsService.getAllRiskWarnings();
//        //  查询出各个区域的用电量 返回饼图数据结构
//        List<ElectricityByArea> electricityByAreaList = itElectricityDataService.getElectricityByAreaWithinTime(startTime, endTime);
//        // 按小时返回 报警总数 处置总数
//        List<HourlyData> hourlyDataList = alarmEventService.getHourlyDataQuery(startTime, endTime);
//        List<HourlyData> hourlyDataList1 = alarmEventService.getHourlyDataQuery1(startTime, endTime);
//        //  煤矿设备检测 运行时长从此刻到开始时间做差
//        List<ElectricityData> electricityDataList = itElectricityDataService.getElectricityData();

    }
}
